<?php
// +----------------------------------------------------------------------
// | 零云 [ 简单 高效 卓越 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.lingyun.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;

use Think\Page;

/**
 * 文章控制器
 * @author jry <598821125@qq.com>
 */
class FieldController extends AdminController
{
    /**
     * 默认方法
     * @author jry <598821125@qq.com>
     */
   

    public function index()
    {
        $uid = session('user_auth.uid');
        $user_group   = D('Admin/Access')->getFieldByUid($uid, 'group'); 
        // 搜索
        $keyword         = I('keyword', '', 'string');
        $condition       = array('like', '%' . $keyword . '%');
        //$map['name|mobile'] = array($condition, $condition, '_multi' => true);

        
        //$map['status'] = array('egt', '0'); // 禁用和正常状态
        
        $post_object   = D('Admin/Field');

        $p             = !empty($_GET["p"]) ? $_GET['p'] : 1;
        $map['name'] = array($condition);

        
        $map['status'] = array('egt', '0'); // 禁用和正常状态
        $post_object   = D('Admin/Field');
        $data_list     = $post_object
            ->page($p, C('ADMIN_PAGE_ROWS'))
            ->where($map)
            ->order('id desc')
            ->select();
        $page = new Page(
            $post_object->where($map)->count(),
            C('ADMIN_PAGE_ROWS')
        );
        

        $builder = new \Common\Builder\ListBuilder();
        $builder->addTopButton('addnew', array('href' => U('add'))) // 添加新增按钮
            
            ->addRightButton('edit'); // 添加编辑按钮
            $builder->setSearch('请输入名称', U('index'))
            ->addRightButton('forbid');
        //$('.item_atime').last().after($('.item_atime').clone(true));
            //$('.item_atime').after($('.item_atime').clone(true));
            //$('.item_atime:last').after($('.item_atime:last').clone(true));

        $builder->setMetaTitle('场地列表') // 设置页面标题
            ->addTableColumn('id', 'ID')
            
            ->addTableColumn('name', '场地名')
            //->addTableColumn('brand_name', '品牌名')
            ->addTableColumn('create_time', '创建时间', 'time')
            ->addTableColumn('update_time', '更新时间', 'time')
            ->addTableColumn('status', '状态', 'status')
            ->addTableColumn('right_button', '操作', 'btn')
            ->setTableDataList($data_list) // 数据列表
            ->setTableDataPage($page->show()) // 数据列表分页
            
            //->addRightButton('delete') // 添加删除按钮
            ->display();
    }

    public function add()
    {
        if (IS_POST) {
            $post_object = D('Admin/Field');
            $data        = $post_object->create(format_data());
            if ($data) {
                $id = $post_object->add();
                if ($id) {
                    $this->success('新增成功', U('index'));
                } else {
                    $this->error('新增失败');
                }
            } else {
                $this->error($post_object->getError());
            }
        } else {

            // 使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('新增品牌') // 设置页面标题
                ->setPostUrl(U('add')) // 设置表单提交地址
                
                //->addFormItem('post_id','select','活动名','活动名',select_list_as_tree('Admin/Post'))
                ->addFormItem('name','text','名称','名称')
                ->addFormItem('content','kindeditor','内容','内容')
                
                //->addFormItem('num', 'select', '场次(1000元/人次)', '场次',array('5' => 5, '10' => 10,'15'=>15,'20'=>20),'','id=num')
                ->display();
        }
    }

    /**
     * 编辑文章
     * @author jry <598821125@qq.com>
     */
    public function edit($id)
    {
        if (IS_POST) {
            $post_object = D('Admin/Field');
            $data        = $post_object->create(format_data());
            if ($data) {

                

                $id = $post_object->save();
                if ($id !== false) {
                    $this->success('更新成功', U('index'));
                } else {
                    $this->error('更新失败');
                }
            } else {
                $this->error($post_object->getError());
            }
        } else {
            // 使用FormBuilder快速建立表单页面。
            $uid = session('user_auth.uid');
            $user_group   = D('Admin/Access')->getFieldByUid($uid, 'group');
            if ($user_group==3) {
                $map['uid'] = $uid;
            }

            $map['status'] = array('egt', '0'); // 禁用和正常状态

            $access_object = D('Access');
            $data_list     = $access_object
            ->where($map)
            ->order('sort asc,id asc')
            ->select();
            
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('编辑品牌') // 设置页面标题
                ->setPostUrl(U('edit')) // 设置表单提交地址
                ->addFormItem('id', 'hidden', 'ID', 'ID')
                ->addFormItem('name','text','名称','名称')
                ->addFormItem('content','kindeditor','内容','内容')
                
                ->setFormData(D('Admin/Field')->find($id))
                ->display();
        }
    }

    
    
}
